package com.epoch.servicecourse.service.impl;

import com.epoch.servicecourse.bean.Course;
import com.epoch.servicecourse.bean.StuCourse;
import com.epoch.servicecourse.dao.CourseDao;
import com.epoch.servicecourse.dao.StuCourseDao;
import com.epoch.servicecourse.service.StuCourseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.AssertUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;

/**
 * @author mi
 */
@Service
@Slf4j
public class StuCourseServiceImpl implements StuCourseService {
    @Resource
    private StuCourseDao stuCourseDao;

    @Autowired
    private CourseDao courseDao;
    @Override
    public void saveStuCourse(StuCourse stuCourse){
        //非空校验，判断课程号与用户编号是否为空
        AssertUtil.isTrue(StringUtils.isBlank(stuCourse.getCourseId()),"课程号不能为空！");
        AssertUtil.isTrue(StringUtils.isBlank(stuCourse.getCsId()),"用户编号不能为空！");
        //添加课程，判断受影响的行数
        AssertUtil.isTrue(stuCourseDao.saveStuCourse(stuCourse)<1,"课程收藏失败！");
        stuCourseDao.addCourseInNum(stuCourse.getCourseId());
    }
    @Override
    public void deleteStuCourse(StuCourse stuCourse){
        //非空校验，判断课程号与用户编号是否为空
        AssertUtil.isTrue(StringUtils.isBlank(stuCourse.getCourseId()),"课程号不能为空！");
        AssertUtil.isTrue(StringUtils.isBlank(stuCourse.getCsId()),"用户编号不能为空！");
        //删除课程，判断受影响的行数
        AssertUtil.isTrue(stuCourseDao.deleteStuCourse(stuCourse)<1,"课程删除失败！");
        stuCourseDao.reduceCourseInNum(stuCourse.getCourseId());
    }
    @Override
    public List<Object> getStuCourseById(String csId){
        //非空校验，判断用户编号是否为空
        AssertUtil.isTrue(StringUtils.isBlank(csId),"用户编号不能为空！");
        //返回课程
        List<Course> courseList = stuCourseDao.getStuCourseById(csId);
        List<Object> resultList = new ArrayList<>();
        for (Course list : courseList) {
            HashMap<String, String> courseMap = new HashMap(8);
            courseMap.put("courseName", list.getCourseName());
            courseMap.put("courseIntro", list.getCourseIntro());
            byte[] byteImage = courseDao.getCourseById(list.getCourseId()).getPhoto();
            String photoType = courseDao.getCourseById(list.getCourseId()).getPhotoType();
            if (photoType != null) {
                System.out.println(photoType);
                String baseStr = "data:image/" + photoType + ";base64," + Base64.getEncoder().encodeToString(byteImage);
                courseMap.put("courseImage", baseStr);
            } else {
                courseMap.put("courseImage", null);
            }
            courseMap.put("courseInNum", Integer.toString(list.getCourseInNum()));
            resultList.add(courseMap);
        }
        return resultList;
    }

    @Override
    public boolean getFlag(String csId, String courseId) {
        return stuCourseDao.getSCById(csId, courseId) == null;
    }
}
